<template>
  <div class="page-header" v-if="article">
    <HeaderCom isDetail />
    <div class="page-title">
      <h1 class="article-title">{{ article.title }}</h1>
      <div class="article-meta">
        <div class="first-meta">
          <span>
            <svg-icon icon-class="calendar" style="margin-right: 15px"></svg-icon> <span class="text">发表于</span>
            {{ formatDate(article.createTime) }}
          </span>
          <span class="item" v-if="article.updateTime">
            <svg-icon icon-class="update" style="margin-right: 15px"></svg-icon> <span class="text">更新于</span>
            {{ formatDate(article.updateTime) }}
          </span>
          <!-- <span class="item">
            <svg-icon icon-class="eye" style="margin-right: 15px"></svg-icon> <span class="text">阅读量 </span>
            {{ article.viewCount }}
          </span> -->
        </div>
        <div class="second-meta">
          <span>
            <svg-icon icon-class="edit" size="18px" style="margin-right: 15px"></svg-icon>
            <span class="text">字数统计 </span>{{ count(wordNum) }} 字
          </span>
          <span class="item">
            <svg-icon icon-class="clock" style="margin-right: 15px"></svg-icon> <span class="text">阅读时长 </span>
            {{ readTime }} 分钟
          </span>
          <span class="item">
            <svg-icon icon-class="category" style="margin-right: 15px"></svg-icon>{{ article.categoryName }}
          </span>
        </div>
      </div>
    </div>
  </div>
  <div class="bg">
    <div class="main-container" v-if="article">
      <div class="left-container">
        <div class="article-container">
          <v-md-preview class="md" ref="articleRef" v-viewer :text="article.content"></v-md-preview>
          <div class="article-post">
            <div class="copyright">
              <ul>
                <li class="author">
                  <svg-icon
                    icon-class="author"
                    size="18px"
                    style="margin-right: 9px; position: relative; top: 5px"
                  ></svg-icon>
                  <strong>本文作者： </strong>{{ user.nickname }}
                </li>
                <li class="link">
                  <svg-icon
                    icon-class="article_link"
                    size="18px"
                    style="margin-right: 9px; position: relative; top: 5px"
                  ></svg-icon>
                  <strong>本文链接：</strong>
                  <a :href="articleHref">{{ articleHref || '-' }}</a>
                </li>
                <li class="license">
                  <svg-icon
                    icon-class="article_share"
                    size="16px"
                    style="margin-right: 9px; position: relative; top: 5px"
                  ></svg-icon>
                  <strong>版权声明： </strong>本站所有文章除特别声明外，转载请注明文章出处！
                </li>
              </ul>
            </div>
            <CommentList :comment-type="commentType"></CommentList>
          </div>
        </div>
      </div>
      <div class="right-container">
        <div class="side-card">
          <Catalog v-if="articleLoaded" :domRef="articleRef"></Catalog>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getArticle, updateViewCount } from '@/api/article.js';
import Catalog from '@/components/Catalog/index.vue';
import CommentList from '@/components/Comment/CommentList.vue';
import SvgIcon from '@/components/svgIcon/index.vue';
import HeaderCom from '@/pages/components/header/index.vue';
import { useUsersStore } from '@/store/user';
import { formatDate } from '@/utils/date';
import { storeToRefs } from 'pinia';
import { onMounted, reactive, ref, toRefs } from 'vue';
import { useRoute } from 'vue-router';

const { user } = storeToRefs(useUsersStore());
const route = useRoute();
const articleHref = window.location.href;

const articleRef = ref();

const data = reactive({
  articleLoaded: false,
  wordNum: 0,
  readTime: 0,
  commentType: 1,
  article: {
    id: 0,
    articleTitle: '',
    content: '',
    viewCount: 0,
    createTime: '',
    updateTime: '',
  },
});
const { articleLoaded, wordNum, readTime, commentType, article } = toRefs(data);
const count = (value) => {
  if (value >= 1000) {
    return (value / 1000).toFixed(1) + 'k';
  }
  return value;
};
const deleteHTMLTag = (content) => {
  return content
    .replace(/<\/?[^>]*>/g, '')
    .replace(/[|]*\n/, '')
    .replace(/&npsp;/gi, '');
};

// 获取文章详情
const getArticleDetail = () => {
  getArticle(Number(route.params.id)).then(({ data }) => {
    article.value = data;
    document.title = article.value.title;
    wordNum.value = deleteHTMLTag(article.value.content).length;
    readTime.value = Math.round(wordNum.value / 400);
    articleLoaded.value = true;
  });
};

// 更新阅读量
const updateViewCountApi = () => {
  updateViewCount(route.params.id);
};
onMounted(() => {
  getArticleDetail();
  updateViewCountApi();
});
</script>

<style lang="scss" scoped>
.page-header {
  padding: 60px 0 0 130px;
  position: relative;
  width: 100%;
  height: 21vh;
  color: var(--grey-7);
  display: flex;
  align-items: center;

  .article-title {
    letter-spacing: 5px;
    text-align: center;
    font-weight: bold;
    color: var(--grey-7);
    font-size: 30px;
    line-height: 30px;
    margin: 0;
  }

  .page-title {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    font-size: 20px;
    font-weight: 600;
    height: 30px;
    line-height: 30px;
  }
}

.article-container {
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 20px var(--box-bg-shadow);
}

.article-post {
  margin: 0 40px;
  padding-bottom: 20px;
}

.article-meta {
  flex-direction: column;
  font-size: 16px;
  margin-top: 20px;

  .item {
    margin-left: 13px;
  }
}

.tag-share {
  display: flex;
  align-items: center;

  .share-info {
    margin-left: auto;
  }
}

.reward {
  margin: 25px auto;
  padding: 13px 0;
  text-align: center;

  .btn {
    border-radius: 10px;
    color: var(--grey-0);
    cursor: pointer !important;
    padding: 0 19px;
    font: inherit;
  }

  .like-btn-active {
    background: var(--primary-color);
  }

  .like-btn {
    background: #999;
  }

  .reward-btn {
    position: relative;
    margin-left: 20px;
    background: var(--primary-color);
  }

  .tea {
    font-size: 0.8125em;
    color: var(--grey-5);
    margin-top: 10px;
  }
}

.reward-all {
  display: flex;
  align-items: center;
}

.reward-img {
  width: 130px;
  height: 130px;
  display: block;
}

.reward-desc {
  margin: -5px 0;
  color: #858585;
  text-align: center;
}

.copyright {
  font-size: 1em;
  padding: 20px 40px;
  margin-bottom: 30px;
  border-radius: 13px;
  background: var(--grey-2);
  color: var(--grey-6);
}

.post-nav {
  display: flex;
  margin-bottom: 30px;
  border-radius: 13px;
  overflow: hidden;

  .item {
    width: 50%;
  }

  .post-cover {
    display: flex;
    flex-direction: column;
    color: var(--header-text-color);
    padding: 25px 30px;
    background-size: cover;
    animation: blur 0.8s ease-in-out forwards;

    &::before {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      background: linear-gradient(135deg, #434343, #000);
      opacity: 0.5;
      transition: all 0.2s ease-in-out 0s;
      z-index: -1;
      top: 0;
      left: 0;
    }
  }

  .post-last-next {
    font-size: 17px;
  }
}

.right-container {
  position: sticky;
  top: 80px;
  min-width: 200px;
  max-width: 250px;
  margin-left: 16px;
}

.post-cover:hover::before {
  opacity: 0.4;
}

@media (max-width: 767px) {
  .article-title {
    font-size: 30px;
  }

  .article-meta .text {
    display: none;
  }

  .article-post {
    margin: 0 10px;
  }

  .post-nav {
    flex-direction: column;
  }

  .post-nav .item {
    width: 100%;
  }

  .reward-img {
    width: 105px;
    height: 105px;
  }
}
</style>
